package string;

public class Multiply {
    public static String code(String num1, String num2) {
        int m = num1.length(), n = num2.length();
        char[] n1 = new StringBuffer(num1).reverse().toString().toCharArray();
        char[] n2 = new StringBuffer(num2).reverse().toString().toCharArray();
        // if ((n1.length == 1 && n1[0] == '0') || (n2.length == 1 && n2[0] == '0')){
        //     return "0";
        // }
        int[] tmp = new int[m + n - 1];

        //累加操作
        for (int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                tmp[i + j] += (n1[i] - '0') * (n2[j] - '0');
            }
        }

        //进位操作
        int cur = 0, t = 0;
        StringBuffer ret = new StringBuffer();
        while (cur < m + n - 1 || t != 0){
            if (cur < m + n - 1){
                t += tmp[cur++];
            }

            ret.append((char)(t % 10 + '0'));
            t /= 10;
        }

        // while (ret.length() > 1 && ret.charAt(ret.length() - 1) == '0'){
        //     ret.deleteCharAt((ret.length() - 1));
        // }

        return ret.reverse().toString();
    }

    public static void main(String[] args) {
        Multiply.code("123", "456");
    }
}
